﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Text;
using HKEA.CMS.Core.BLL;
using HKEA.CMS.Core.Models;
using HKEA.CMS.Core.Utility;
using HKEA.CMS.Core.BLL.Tbl;
namespace WebApplication.ServerAshxFile
{
    /// <summary>
    /// GetContactPerson 的摘要说明
    /// </summary>
    public class GetContactPerson : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            int uid = 0;
            StringBuilder strBuilder = new StringBuilder();
            string returnStr = string.Empty;
            if (context.Request.QueryString["uid"] != null)
            {
                if (int.TryParse(context.Request.QueryString["uid"], out uid))
                {
                    string strFormat = "{{\"uid\":\"{0}\",\"type\":\"{1}\",\"name\":\"{2}\"}}";
                    tblCust_ContactPerson cpENT = new tblCust_ContactPerson();
                    tblCust_ContactPersonBLL cpBLL = new tblCust_ContactPersonBLL();
                    tblCust_Branch branchENT = new tblCust_Branch();
                    tblCust_BranchBLL branchBLL = new tblCust_BranchBLL();
                    tblCustomerBLL customerBLL = new tblCustomerBLL();
                    tblCustomer customerENT = new tblCustomer();
                    customerENT.CustomerUID = uid;
                    customerENT = customerBLL.GetEntity(customerENT);
                    if (customerENT != null)
                    {
                        strBuilder.Append(string.Format(strFormat,customerENT.CustomerUID,"cust",customerENT.Name_Eng + " " + customerENT.Name_Chi));
                        strBuilder.Append(",");
                    }
                    cpENT.CustomerUID = uid;              
                    var listCP = cpBLL.GetListByENT(cpENT);
                    int countCP = listCP.Count();
                    if (countCP > 0)
                    {
                        foreach (var tempCP in listCP)
                        {
                            var TempENT = tempCP.tblContactPerson;
                            strBuilder.Append(string.Format(strFormat, tempCP.ContactPersonUID, "cp", TempENT.ChristianName + " " + TempENT.FirstName_Eng + "" + TempENT.LastName_Eng + " " + TempENT.FirstName_Chi + " " + TempENT.LastName_Chi));
                            strBuilder.Append(",");
                        }
                    }
                    branchENT.CustomerUID = uid;
                    var listBranch = branchBLL.GetListByENT(branchENT);
                    int countBranch = listBranch.Count();
                    if (countBranch > 0)
                    {
                        foreach (var tempbranch in listBranch)
                        {
                            strBuilder.Append(string.Format(strFormat, tempbranch.CustBranchUID, "branch", tempbranch.BranchName_Eng + " " + tempbranch.BranchName_Chi ));
                            strBuilder.Append(",");
                        }
                    }
                    returnStr = strBuilder.ToString();
                    if (!string.IsNullOrEmpty(returnStr))
                    {
                        returnStr = returnStr.Substring(0, returnStr.Length - 1);
                    }
                }
                else
                {
                    returnStr = ResourceManage.GetResourceString("msg200001");
                }
            }
            else
            { returnStr = ResourceManage.GetResourceString("msg200002"); }
            context.Response.Write(returnStr);
            context.Response.Flush();
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}